/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.CourierModel;
import com.hykj.xxltest.service.CourierService;
import com.hykj.xxltest.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author innel
 * @date 2018-07-09 18:41:56
 */
@Controller
@RequestMapping("/courier")
@Api(description = "")
public class CourierController {

    private static final Logger logger = Logger.getLogger(CourierController.class);

    @Autowired
    private CourierService courierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            response = CourierModel.class,
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "0正常 -1冻结 -10删除")
                        @RequestParam(required = false)
                                Integer status,
                        @ApiParam(value = "积分")
                        @RequestParam(required = false)
                                Integer point,
                        @ApiParam(value = "0 未认证 1 认证中 2=认证通过 -1 = 认证失败")
                        @RequestParam(required = false)
                                Integer isauth,
                        @ApiParam(value = "等级")
                        @RequestParam(required = false)
                                Integer lev,
                        @ApiParam(value = "是否可以接单 0 否 1是")
                        @RequestParam(required = false)
                                Integer isready,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("status", status);
        params.put("point", point);
//        params.put("isauth", isauth);
//        params.put("lev", lev);
        params.put("isauth", isauth);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(courierService.getDList(params));
        }
        PageInfo<CourierModel> p = courierService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
//    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取",
            notes = "备注：",
            response = CourierModel.class,
            httpMethod = "POST")
    public AppBack info(
            @ApiParam(value = "的ID", required = true)
            @RequestParam Integer id) {
        CourierModel courier = courierService.getByIdD(id);
        if (courier == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(courier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(
            @ApiParam(value = "昵称", required = true)
            @RequestParam
                    String nickname,


            @ApiParam(value = "头像", required = true)
            @RequestParam
                    String headPhoto,


            @ApiParam(value = "token", required = true)
            @RequestParam
                    String token,


            @ApiParam(value = "0正常 -1冻结 -10删除", required = true)
            @RequestParam
                    Integer status,


            @ApiParam(value = "手机号码", required = true)
            @RequestParam
                    String phone,


            @ApiParam(value = "密码", required = true)
            @RequestParam
                    String password,


            @ApiParam(value = "姓名", required = true)
            @RequestParam
                    String realname,


            @ApiParam(value = "生日   例1999-05-19", required = true)
            @RequestParam
                    String birthday,


            @ApiParam(value = "性别 男/女", required = true)
            @RequestParam
                    String sex,


            @ApiParam(value = "身体状态  例 健康,自然", required = true)
            @RequestParam
                    String bodyTag,


            @ApiParam(value = "期望标签 ", required = true)
            @RequestParam
                    String expTag,


            @ApiParam(value = "wechatOpenid", required = true)
            @RequestParam
                    String wechatOpenid,


            @ApiParam(value = "qqOpenid", required = true)
            @RequestParam
                    String qqOpenid,


            @ApiParam(value = "weboOpenid", required = true)
            @RequestParam
                    String weboOpenid,


            @ApiParam(value = " ", required = true)
            @RequestParam
                    String email,


            @ApiParam(value = "积分", required = true)
            @RequestParam
                    Integer point,


            @ApiParam(value = "余额", required = true)
            @RequestParam
                    BigDecimal amount,


            @ApiParam(value = "0 未认证 1 认证中 2=认证通过 -1 = 认证失败", required = true)
            @RequestParam
                    Integer isauth,


            @ApiParam(value = "等级", required = true)
            @RequestParam
                    Integer lev,


            @ApiParam(value = "押金", required = true)
            @RequestParam
                    BigDecimal bzamount,


            @ApiParam(value = "支付密码", required = true)
            @RequestParam
                    String paypass,


            @ApiParam(value = "所在地址", required = true)
            @RequestParam
                    String address,


            @ApiParam(value = "是否可以接单 0 否 1是", required = true)
            @RequestParam
                    Integer isready,

            @ApiParam(value = "选填参数控制 默认不填", hidden = true)
            @RequestParam(required = false)
                    Integer extra) {
        CourierModel courier = new CourierModel();

        courier.setNickname(nickname);


        courier.setHeadPhoto(headPhoto);


        courier.setToken(token);


        courier.setStatus(status);


        courier.setPhone(phone);


        courier.setPassword(password);


        courier.setRealname(realname);


        courier.setBirthday(birthday);


        courier.setSex(sex);


        courier.setBodyTag(bodyTag);


        courier.setExpTag(expTag);


        courier.setWechatOpenid(wechatOpenid);


        courier.setQqOpenid(qqOpenid);


        courier.setWeboOpenid(weboOpenid);


        courier.setEmail(email);


        courier.setPoint(point);


        courier.setAmount(amount);


        courier.setIsauth(isauth);


        courier.setLev(lev);


        courier.setBzamount(bzamount);


        courier.setPaypass(paypass);


        courier.setAddress(address);


        courier.setIsready(isready);


        courierService.create(courier);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "昵称")
                          @RequestParam(required = false)
                                  String nickname,
                          @ApiParam(value = "头像")
                          @RequestParam(required = false)
                                  String headPhoto,
                          @ApiParam(value = "token")
                          @RequestParam(required = false)
                                  String token,
                          @ApiParam(value = "0正常 -1冻结 -10删除")
                          @RequestParam(required = false)
                                  Integer status,
                          @ApiParam(value = "手机号码")
                          @RequestParam(required = false)
                                  String phone,
                          @ApiParam(value = "密码")
                          @RequestParam(required = false)
                                  String password,
                          @ApiParam(value = "姓名")
                          @RequestParam(required = false)
                                  String realname,
                          @ApiParam(value = "生日   例1999-05-19")
                          @RequestParam(required = false)
                                  String birthday,
                          @ApiParam(value = "性别 男/女")
                          @RequestParam(required = false)
                                  String sex,
                          @ApiParam(value = "身体状态  例 健康,自然")
                          @RequestParam(required = false)
                                  String bodyTag,
                          @ApiParam(value = "期望标签 ")
                          @RequestParam(required = false)
                                  String expTag,
                          @ApiParam(value = "wechatOpenid")
                          @RequestParam(required = false)
                                  String wechatOpenid,
                          @ApiParam(value = "qqOpenid")
                          @RequestParam(required = false)
                                  String qqOpenid,
                          @ApiParam(value = "weboOpenid")
                          @RequestParam(required = false)
                                  String weboOpenid,
                          @ApiParam(value = " ")
                          @RequestParam(required = false)
                                  String email,
                          @ApiParam(value = "积分")
                          @RequestParam(required = false)
                                  Integer point,
                          @ApiParam(value = "余额")
                          @RequestParam(required = false)
                                  BigDecimal amount,
                          @ApiParam(value = "0 未认证 1 认证中 2=认证通过 -1 = 认证失败")
                          @RequestParam(required = false)
                                  Integer isauth,
                          @ApiParam(value = "等级")
                          @RequestParam(required = false)
                                  Integer lev,
                          @ApiParam(value = "押金")
                          @RequestParam(required = false)
                                  BigDecimal bzamount,
                          @ApiParam(value = "支付密码")
                          @RequestParam(required = false)
                                  String paypass,
                          @ApiParam(value = "所在地址")
                          @RequestParam(required = false)
                                  String address,
                          @ApiParam(value = "是否可以接单 0 否 1是")
                          @RequestParam(required = false)
                                  Integer isready,
                          @ApiParam(value = "身份证正面 ")
                              @RequestParam(required = false)
                                      String idcard,
                          @ApiParam(value = "身份证反面 ")
                              @RequestParam(required = false)
                                      String idcard1,
                          @ApiParam(value = "的ID", required = true)
                          @RequestParam int id) {
        CourierModel courier = new CourierModel();
        courier.setNickname(nickname);
        courier.setHeadPhoto(headPhoto);
        courier.setToken(token);
        courier.setStatus(status);
        courier.setPhone(phone);
        courier.setPassword(password);
        courier.setRealname(realname);
        courier.setBirthday(birthday);
        courier.setSex(sex);
        courier.setBodyTag(bodyTag);
        courier.setExpTag(expTag);
        courier.setWechatOpenid(wechatOpenid);
        courier.setQqOpenid(qqOpenid);
        courier.setWeboOpenid(weboOpenid);
        courier.setEmail(email);
        courier.setPoint(point);
        courier.setAmount(amount);
        courier.setIsauth(isauth);
        courier.setLev(lev);
        courier.setBzamount(bzamount);
        courier.setPaypass(paypass);
        courier.setAddress(address);
        courier.setIsready(isready);
        courier.setId(id);
        courier.setIdcard(idcard);
        courier.setIdcard1(idcard1);
        courierService.update(courier);
        return new AppBack();
    }
    /**
     * 工人审核
     */
    @RequestMapping("/check")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "审核",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack check(@ApiParam(value = "的ID", required = true)
                          @RequestParam
                                  int id,
                         @RequestParam int status
    ) {

        CourierModel newCourier = new CourierModel();
        newCourier.setIsauth(status);
        newCourier.setId(id);
        courierService.update(newCourier);
        return new AppBack();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "的ID", required = true)
                          @RequestParam
                                  int id
                          ) {

        CourierModel newCourier = new CourierModel();
        newCourier.setStatus(-10);
        newCourier.setId(id);
        courierService.update(newCourier);
        return new AppBack();
    }

    /**
     * 微信登录
     */
    @RequestMapping("/wechatLogin")
    @ResponseBody
    @ApiOperation(
            value = "微信登录",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack wechatLogin(
            @ApiParam(value = "openid",required = true)
            @RequestParam String openid,
            @ApiParam(value = "昵称",required = true)
            @RequestParam String nickname,
            @ApiParam(value = "头像",required = true)
            @RequestParam String headphoto,
            @ApiParam(value = "手机号",required = false)
            @RequestParam String phone
    ){

        HashMap condition = new HashMap();
        condition.put("wechatOpenid",openid);
        List<CourierModel> courierModels = courierService.getListLite(condition);
        CourierModel courierModel;
        if (courierModels.size() == 0){
            courierModel = new CourierModel();
            courierModel.setWechatOpenid(openid);
            courierModel.setStatus(0);
           BigDecimal flag = new BigDecimal(0);
           courierModel.setAmount(flag);
           courierModel.setHeadPhoto(headphoto);
           courierModel.setNickname(nickname);
           courierService.create(courierModel);

        }else {
            courierModel = courierModels.get(0);
        }
        CourierModel newCourier = new CourierModel();
        String token = TokenUtil.getInstance().generateTokenUUID();
        newCourier.setToken(token);
        newCourier.setId(courierModel.getId());
        courierService.update(newCourier);
        return  new AppBack()
                .add("courier",courierModel)
                .add("token",token);
    }

}
